// pages/Tabbar/My/bankCard/bankCardList.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;

import { getBankCard , deleteBankCard } from '../../../../api/card';

Page({
  // 获取银行卡数据
  getData(){
    let _this = this;
    getBankCard({ page: this.data.page,})
    .then(res => {
      if(res.success){
        _this.setData({
          bank_card_datas: res.data,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  //添加银行卡
  addBankCard(){
    wx.navigateTo({
      url:"addBankCard",
      events: {
        /*数据监听*/
        someEvent: function() {
          /*刷新*/
          console.log("刷新");
        }
      },
    });
  },
  // 删除银行卡
  delete_card(e){
    let _this = this;
    wx.showModal({
      title: '温馨提示',
      content: '是否删除当前银行卡',
      success (res) {
        if (res.confirm) {
          console.log(e)
          const id = e.target.dataset.id || e.currentTarget.dataset.id;
          console.log(id,'id')
          deleteBankCard(id)
          .then(res => {
            if(res.success){
              wx.showToast({
                title: '删除成功',
                icon: 'none',
              })
              _this.getData();
            }else{
              wx.showToast({
                title: res.errMessage,
                icon: 'none',
              })
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    bank_card_height:wx.getWindowInfo().screenHeight-(wx.getWindowInfo().screenHeight-wx.getWindowInfo().safeArea.bottom)-wx.getWindowInfo().statusBarHeight-44-12-40-5,
    // noMoreData:false,
    bank_card_datas:[],
    token: wx.getStorageSync('token'),
    userRealnameStatus: wx.getStorageSync('realNameStatus'),
    assetsAddress: assetsAddress,
    showPrivacyAuthor: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      token: wx.getStorageSync('token'),
      userRealnameStatus: wx.getStorageSync('realNameStatus'),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userRealnameStatus: wx.getStorageSync('realNameStatus'),
    })
    this.getData();
    var _this = this;
    if (wx.onNeedPrivacyAuthorization){
      wx.onNeedPrivacyAuthorization((resolve) => {
        console.log('触发弹窗')
        // 触发弹窗
        _this.setData({
          showPrivacyAuthor: true,
        })
        console.log(_this.data.showPrivacyAuthor,'showPrivacyAuthor')
        _this.resolvePrivacyAuthorization = resolve
        console.log(resolve,resolve);
      })
    }  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  myChildEvent(e){
    console.log(e,'子组件触发')
    this.setData({
      token: wx.getStorageSync('token')
    })
  },
  handleAgreePrivacyAuthorizationFun(){
    console.log('同意之后的回调');
  },
  disagreePrivacyAuthorization(){
    this.resolvePrivacyAuthorization({ event: 'disagree' });
  },
})